package config

import (
	"encoding/json"
	"io/ioutil"
)

type Config struct {
	DbHost     string `json:"db_host"`
	DbPort     string `json:"db_port"`
	DbName     string `json:"db_name"`
	DbUser     string `json:"db_user"`
	DbPassword string `json:"db_password"`
}

var (
	// GlobalConfig 单例
	GlobalConfig *Config
)

// InitConfig 加载配置
func InitConfig(filename string) (err error) {
	var (
		content []byte
		conf    Config
	)

	//读取配置
	if content, err = ioutil.ReadFile(filename); err != nil {
		return err
	}

	//json反序列化
	if err = json.Unmarshal(content, &conf); err != nil {
		return err
	}
	//赋值
	GlobalConfig = &conf
	return
}
